import 'package:library_base/res/edge.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/num_util.dart';
import 'package:library_base/utils/sp_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:module_common/page/setting_weighing/setting_weighing_controller.dart';
import 'package:flutter/material.dart';
import 'package:library_base/base/base_app_state_page.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:get/get.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/ext/bool_ext.dart';

class SettingWeighingPage extends StatefulWidget {
  const SettingWeighingPage({Key? key}) : super(key: key);

  @override
  _SettingWeighingPageState createState() => _SettingWeighingPageState();
}

class _SettingWeighingPageState
    extends BaseState<SettingWeighingPage, SettingWeighingController> {
  late bool isJin;

  TextEditingController textEditingController = TextEditingController();

  @override
  SettingWeighingController registerController() =>
      Get.find<SettingWeighingController>();

  @override
  void onCreate() {
    setBackgroundColor(ThemeUtil.getTheme().secondaryBgColor());
    setTitleVisible(true);
    setTitle(MultiLanguage.settingWeighing.tr);
    isJin = SPUtil.isJin;
    textEditingController.text = isJin
        ? (SPUtil.skinWeight * 2).toString()
        : SPUtil.skinWeight.toString();
  }

  @override
  Widget? getMainWidget(BuildContext context) {
    return Container(
      color: ThemeUtil.getTheme().colorScheme.secondary,
      child: Column(
        children: [
          Container(
            padding: edgeV36H24 as EdgeInsets,
            child: Row(
              children: [
                Text(
                    "${MultiLanguage.tareDeduction.tr}(${isJin.unitStrZh()}):",
                    style: normalTextColor34),
                gapW18,
                Expanded(
                    child: TextField(
                  controller: textEditingController,
                  keyboardType: TextInputType.number,
                )),
              ],
            ),
          ),
          Container(
            padding: edgeV36H24 as EdgeInsets,
            child: Row(
              children: [
                Expanded(
                    child: Text(MultiLanguage.defaultWeightUnit.tr,
                        style: normalTextColor28)),
                Text(MultiLanguage.kilogram.tr),
                Switch(
                    activeColor: ThemeUtil.getTheme().primaryColor,
                    activeTrackColor:
                        ThemeUtil.getTheme().primaryColor.withOpacity(0.3),
                    value: isJin,
                    onChanged: (value) {
                      setState(() {
                        if (isJin != value) {
                          isJin = value;

                          if (NumUtil.isNumber(textEditingController.text)) {
                            double skinWeight =
                                double.parse(textEditingController.text);
                            if (isJin) {
                              textEditingController.text = "${skinWeight * 2}";
                            } else {
                              textEditingController.text =
                                  "${NumUtil.divide(skinWeight, 2)}";
                            }
                          }
                        }
                      });
                    }),
                Text(MultiLanguage.jin.tr),
              ],
            ),
          ),
          Expanded(
              child: Container(
            alignment: Alignment.bottomCenter,
            child: GradientButton(
              ThemeUtil.getTheme(),
              width: double.infinity,
              padding: edgeAll24 as EdgeInsets,
              margin: edgeAll24 as EdgeInsets,
              text: MultiLanguage.save.tr,
              onPressed: () {
                Get.back();
                if (textEditingController.text.isNotEmpty) {
                  if (!NumUtil.isNumber(textEditingController.text)) {
                    ToastUtil.normal('请输入数字');
                    return;
                  }
                } else {
                  SPUtil.skinWeight = 0.0;
                }
                SPUtil.isJin = isJin;

                double inputWeight = double.parse(textEditingController.text);
                if(SPUtil.isJin){
                  SPUtil.skinWeight = NumUtil.divide(inputWeight, 2);
                }else{
                  SPUtil.skinWeight = inputWeight;
                }
                ToastUtil.normal(MultiLanguage.saveSuccessfully.tr);
              },
            ),
          )),
          gapH100,
        ],
      ),
    );
  }
}
